package com.duoduo.admin.entity;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;

import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.hibernate.Hibernate;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Entity
@Getter
@Setter
@EntityListeners(AuditingEntityListener.class)
public class SysRole implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
     /**
	 * id
	 */
	private Long id;
     /**
	 * 角色名称
	 */
	private String roleName;
     /**
	 * code码
	 */
	private String roleCode;
     /**
	 * 说明
	 */
	private String roleDesc;
     /**
	 * 删除标识
	 */
	private Boolean delFlag;
     /**
	 * 创建人
	 */
    @CreatedBy
	private String createBy;
     /**
	 * 创建时间
	 */
    @CreatedDate
	private LocalDateTime createTime;
     /**
	 * 修改人
	 */
    @LastModifiedBy
	private String updateBy;
     /**
	 * 更新时间
	 */
    @LastModifiedDate
	private LocalDateTime updateTime;
    
    @ManyToMany(targetEntity = SysMenu.class, fetch = FetchType.LAZY)
    @JoinTable(name = "sys_role_menu", joinColumns = {@JoinColumn(name = "role_id")}, inverseJoinColumns = {@JoinColumn(name = "menu_id")})
    @ToString.Exclude
    private Set<SysMenu> permissions = Collections.emptySet();


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
        SysRole role = (SysRole) o;
        return id != null && Objects.equals(id, role.id);
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }

}
